/**
//编写一个程序，通过填充空格来解决数独问题。 
//
// 数独的解法需 遵循如下规则： 
//
// 
// 数字 1-9 在每一行只能出现一次。 
// 数字 1-9 在每一列只能出现一次。 
// 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。（请参考示例图） 
// 
//
// 数独部分空格内已填入了数字，空白格用 '.' 表示。 
//
// 
//
// 
// 
// 
// 示例： 
//
// 
//输入：board = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".
//",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".
//","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6
//"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[
//".",".",".",".","8",".",".","7","9"]]
//输出：[["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8
//"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],[
//"4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9",
//"6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4",
//"5","2","8","6","1","7","9"]]
//解释：输入的数独如上图所示，唯一有效的解决方案如下所示：
//
//
// 
//
// 
//
// 提示： 
//
// 
// board.length == 9 
// board[i].length == 9 
// board[i][j] 是一位数字或者 '.' 
// 题目数据 保证 输入数独仅有一个解 
// 
// 
// 
// 
// Related Topics 数组 回溯 矩阵 👍 1168 👎 0

*/

package com.xixi.basicAlgroithms.backtracking;

import java.util.ArrayList;
import java.util.List;

public class ID00037SudokuSolver {
public static void main(String[] args) {
Solution solution = new ID00037SudokuSolver().new Solution();
}


//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    boolean[][][] block = new boolean[3][3][9];
    List<int[]> space = new ArrayList<>();
    boolean foundAnswer = false;

    public void solveSudoku(char[][] board) {

        //从第一行第一列开始暴力扫描

        //先扫描一遍，构建block,扫描空格
        for(int i = 0; i < 9; ++i){
            for(int j = 0; j < 9; ++j){
                if (board[i][j] == '.'){ //待填数字
                    space.add(new int[]{i,j});
                }else{ //block中已存在的数字进行标记
                    int blockNum = Integer.parseInt(String.valueOf(board[i][j]));
                    //避免越界，下标减1
                    block[i/3][j/3][blockNum - 1] = true;
                }


            }
        }


        backTracking(space ,0, board);

    }


    public void backTracking(List<int[]> space, int level, char[][] board){
        //判断结束条件
        if(foundAnswer || level == space.size()){
            foundAnswer = true; //防止继续遍历用
            return;
        }
        //逐个空框框决策
        int row = space.get(level)[0];
        int column = space.get(level)[1];

        //每个value实验一下
        for(char value = '1'; value <= '9' ; ++value){
            if(isOk(row,column, value, board)){
                board[row][column] = value; //画上
                block[row/3][column/3][Integer.parseInt(String.valueOf(value)) -1 ] = true;
                backTracking(space,level+1, board); //往下一个格子决策
                //都测试一遍之后发现这不是正确解，回溯
                if(foundAnswer){
                    return;
                }
                board[row][column] = '.'; //擦掉
                block[row/3][column/3][Integer.parseInt(String.valueOf(value)) -1] = false;
            }
        }

    }


    public boolean isOk(int row, int column, char value, char[][] board){
        int i = row;
        int j = 8;
        //1 . 行没有相同值
        while(j >=0){
            if(board[i][j] == value) return false;
            --j;
        }
        //2 .列没有相同值
        i = 8;
        j = column;
        while(i >= 0 ){
            if(board[i][j] == value) return false;
            --i;
        }

        //3 .block中没有相同值
        i = row/3;
        j = column/3;
        int valueIndex = Integer.parseInt(String.valueOf(value)) - 1;
        if(block[i][j][valueIndex]){ //为true证明block中已经存在该值
            return false;
        }

        return true;
    }
}
//leetcode submit region end(Prohibit modification and deletion)




}